// LabelUtils.cpp
#include "LabelUtils.h"

void LabelUtils::setLabelColor(QLabel* label, const QColor& color)
{
    if (!label) return;  // 防止空指针

    QString styleSheet = QString("QLabel { color: %1; }").arg(color.name());
    label->setStyleSheet(styleSheet);
}
void LabelUtils::addFriend(QListWidget* listWidget, const QString& name, const QString& iconPath) {
    QWidget* customWidget = new QWidget();
    QHBoxLayout* layout = new QHBoxLayout(customWidget);

    QLabel* iconLabel = new QLabel();
    QPixmap pixmap(iconPath);
    iconLabel->setPixmap(pixmap.scaled(40, 40, Qt::KeepAspectRatio));
    layout->addWidget(iconLabel);

    QLabel* nameLabel = new QLabel(name);
    nameLabel->setObjectName("nameLabel");  // 设置objectName以便查找
    layout->addWidget(nameLabel);

    layout->setContentsMargins(5, 0, 5, 0);
    layout->setSpacing(10);

    QListWidgetItem* item = new QListWidgetItem(listWidget);
    item->setSizeHint(customWidget->sizeHint());
    listWidget->setItemWidget(item, customWidget);
}
void LabelUtils::removeFriend(QListWidget* listWidget, const QString& name) {
    for (int i = 0; i < listWidget->count(); ++i) {
        QListWidgetItem* item = listWidget->item(i);
        QWidget* customWidget = listWidget->itemWidget(item);
        if (customWidget) {
            QLabel* nameLabel = customWidget->findChild<QLabel*>("nameLabel");
            if (nameLabel && nameLabel->text() == name) {
                delete listWidget->takeItem(i);  // 移除并删除该项
                return;
            }
        }
    }
}
